/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xml;

import commom.VirtualMachine;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.UUID;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;



/**
 *
 * @author gravena
 */
public class Parser {
    private Document doc;

    public Parser(String xml) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db;        
        try {
            /*
             * Write a temp file with the XML content
             */
            File xmlFile = new File("temp" + System.nanoTime());
            xmlFile.createNewFile();

            FileWriter outFile = new FileWriter(xmlFile);
            PrintWriter out = new PrintWriter(outFile);
            out.write(xml);
            out.close();

            db = dbf.newDocumentBuilder();
            doc = db.parse(xmlFile);
            doc.getDocumentElement().normalize();

            xmlFile.delete();
        } catch (ParserConfigurationException ex) {
            System.out.println(ex.getMessage());
        } catch (SAXException ex) {
            System.out.println(ex.getMessage());
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public String getMessageType() {
        NodeList nodeList = doc.getElementsByTagName("message");

        return nodeList.item(0).getAttributes().getNamedItem("type").getNodeValue();
    }

    public String getFileName() {
        NodeList nodeList = doc.getElementsByTagName("name");

        return nodeList.item(0).getTextContent();
    }

    public String getFileSize() {
        NodeList nodeList = doc.getElementsByTagName("size");

        return nodeList.item(0).getTextContent();
    }

    public String getContent() {
        NodeList nodeList = doc.getElementsByTagName("content");

        return nodeList.item(0).getTextContent();
    }

    public long getCheckSum() {
        NodeList nodeList = doc.getElementsByTagName("checksum");

        if(nodeList.getLength() == 0) {
            System.err.println("No checksum field");
            return -1;
        } else {            
            return Long.valueOf(nodeList.item(0).getTextContent());
        }
    }

    public UUID getTestID() {
        NodeList nodeList = doc.getElementsByTagName("TestID");

        if(nodeList.getLength() == 0) {
            System.err.println("No TestID field");
            return null;
        } else {
            return UUID.fromString(nodeList.item(0).getTextContent());
        }
    }

    public ArrayList<VirtualMachine> getVMList() {
        ArrayList<VirtualMachine> vms = new ArrayList<>();

        NodeList nodeList = doc.getElementsByTagName("VM");

        for(int i=0;i<nodeList.getLength();i++) {
            NodeList childs = nodeList.item(i).getChildNodes(); 
            VirtualMachine vm = new VirtualMachine(childs.item(3).getTextContent(), childs.item(5).getTextContent(), childs.item(7).getTextContent(), childs.item(9).getTextContent(), childs.item(11).getTextContent());
            vm.setUUID(UUID.fromString(childs.item(1).getTextContent()));
            vms.add(vm);
        }

        return vms;
    }
    

    public String getStatus() {
        NodeList nodeList = doc.getElementsByTagName("status");

        return nodeList.item(0).getTextContent();
    }
    
    public String getDistribution() {
        NodeList nodeList = doc.getElementsByTagName("Distribution");

        StringTokenizer strToken = new StringTokenizer(nodeList.item(0).getTextContent(), "(");
        if(nodeList.getLength() == 0) {
            System.err.println("No Distribution field");
            return null;
        } else {
            return strToken.nextToken();
        }   
    }
    
    public double[] gerParameters() {
        NodeList nodeList = doc.getElementsByTagName("Distribution");

        StringTokenizer strToken = new StringTokenizer(nodeList.item(0).getTextContent(), "(");
        strToken.nextToken();
        
        String temp = strToken.nextToken();
        String substring = temp.substring(0, temp.length()-1);
        StringTokenizer strTokenComma = new StringTokenizer(substring, ",");
        int nTokens = strTokenComma.countTokens();
        double param[] = new double[nTokens];
        for(int i=0;i<nTokens;i++) {
            param[i] = Double.valueOf(strTokenComma.nextToken());
        }
        if(nodeList.getLength() == 0) {
            System.err.println("No Distribution field");
            return null;
        } else {
            return param;
        }   
    }

    public String getVMName() {
        NodeList nodeList = doc.getElementsByTagName("name");

        return nodeList.item(0).getTextContent();
    }
    
    public UUID getVMId() {
        NodeList nodeList = doc.getElementsByTagName("UUID");

        if(nodeList.getLength() == 0) {
            System.err.println("No UUID field");
            return null;
        } else {
            return UUID.fromString(nodeList.item(0).getTextContent());
        }
    }
    
    public String getBinary() {
        NodeList nodeList = doc.getElementsByTagName("Binary");

        if(nodeList.getLength() == 0) {
            System.err.println("No Binary field");
            return null;
        } else {
            return nodeList.item(0).getTextContent();
        }
    }
    
    public int getSamples() {
        NodeList nodeList = doc.getElementsByTagName("Samples");

        if(nodeList.getLength() == 0) {
            System.err.println("No Samples field");
            return -1;
        } else {
            return Integer.valueOf(nodeList.item(0).getTextContent());
        }
    }
    
}
